package com.example.covid19.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.covid19.common.Dto.goodsDto;
import com.example.covid19.common.Dto.monDto;
import com.example.covid19.common.Dto.stDto;
import com.example.covid19.common.lang.Result;
import com.example.covid19.entity.Goods;
import com.example.covid19.entity.Gstock;
import com.example.covid19.entity.Monnaie;
import com.example.covid19.mapper.GoodsMapper;
import com.example.covid19.mapper.GstockMapper;
import com.example.covid19.mapper.MonnaieMapper;
import com.example.covid19.service.GoodsService;
import com.example.covid19.service.GstockService;
import com.example.covid19.service.ManufacturerService;
import com.example.covid19.service.MonnaieService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;

import static com.google.common.io.Files.getFileExtension;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BaiZhengChun
 * @since 2022-11-30
 */
@RestController
@RequestMapping("/gstock")
public class GstockController {
    private final GoodsService goodsService;
    private final GoodsMapper goodsMapper;
    private final GstockService gstockService;
    private final GstockMapper gstockMapper;
    private final MonnaieService monnaieService;
    private final MonnaieMapper monnaieMapper;

    public GstockController(GoodsService goodsService, GoodsMapper goodsMapper, GstockService gstockService, GstockMapper gstockMapper, ManufacturerService manufacturerService, MonnaieService manufacturerService1, MonnaieService monnaieService, MonnaieMapper monnaieMapper) {
        this.goodsService = goodsService;
        this.goodsMapper = goodsMapper;
        this.gstockService = gstockService;
        this.gstockMapper = gstockMapper;
        this.monnaieService = monnaieService;
        this.monnaieMapper = monnaieMapper;
    }
    @RequestMapping("/stinfo")
    public Result goodsinfo(@RequestParam("pageNo") Integer pageNo, @RequestParam("pagesize")Integer pagesize) throws FileNotFoundException {
        int a = (pageNo-1)*pagesize;
        return Result.success(200,"成功", gstockMapper.gooinfo(a,pagesize));
    }
    @RequestMapping("/stinfo2")
    public Result goodsinfo2(@RequestParam("pageNo") Integer pageNo, @RequestParam("pagesize")Integer pagesize) throws FileNotFoundException {
        int a = (pageNo-1)*pagesize;
        HashMap<Object,Object> map = new HashMap<>(2);
        int k = gstockMapper.selectCount(null);
        int i = gstockMapper.gooinfo(a,pagesize).size();
        List<stDto> list = gstockMapper.gooinfo(a,pagesize);
        System.out.println(list);
        for (int j = 0; j < i; j++) {
            String str = getbase(list.get(j).getPicpath());
            list.get(j).setPicpath(str);
        }
        map.put("count",k);
        map.put("info",list);
        return Result.success(200, "查询成功", map);
    }
    @RequestMapping("/offic")
    public Result offic(@RequestParam("count") Integer count,@RequestParam("picid") String picid) {
        Goods goods = goodsService.getOne(new QueryWrapper<Goods>().eq(
                "picid",picid
        ));
        LocalDateTime dateTime1 = LocalDateTime.now();
        Monnaie monnaie = new Monnaie();
        monnaie.setJour(dateTime1);
        monnaie.setQuantite(count);
        int i = goods.getQuantity();
        goods.setQuantity(i+count);
        Gstock gstock = gstockService.getOne(new QueryWrapper<Gstock>().eq(
                "picid",picid
        ));
        monnaie.setSid(gstock.getSsid());
        int b = gstock.getSsid();
        int k = gstock.getStock();
        if (count>k){
            return Result.fail(400,"没有足够的库存",null);
        }else {
            gstock.setStock(k-count);
            boolean flag = goodsService.updateById(goods)&&gstockService.updateById(gstock)&&monnaieService.save(monnaie);
            if (flag){
//                manl(a,b);
                return Result.success(200,"上获成功",null);
            }
            else {
                return Result.fail(400,"上货失败",null);
            }
        }

    }
    @RequestMapping("/manl")
    public Result manl() {
        HashMap<Object,Object> hashMap = new HashMap<>(2);
        int i = monnaieMapper.selectCount(null);
        hashMap.put("count",i);
        hashMap.put("info",monnaieMapper.moninfo());
        return Result.success(200,"成功",hashMap);
    }
    @RequestMapping("/manl2")
    public Result manl2(@RequestParam("pageNo") Integer pageNo, @RequestParam("pagesize")Integer pagesize) throws FileNotFoundException {
        int a = (pageNo-1)*pagesize;
        List<monDto> list = monnaieMapper.moninfo2(a,pagesize);
        return Result.success(200, "查询成功", list);
    }
    public String getbase(String picid) throws FileNotFoundException {
        File file = new File(picid);
        String name = getFileExtension(String.valueOf(file));
        InputStream in = new FileInputStream(file);
        byte[] data = null;
        try {
//                in = file.openStream();//远程文件
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Base64.Encoder encoder = Base64.getEncoder();
        String code = "data:Image/" + name + ";base64," + encoder.encodeToString(data);
        return code;
    }
    @RequestMapping("/delgo")
    public Result delgo(@RequestParam("sid") Integer sid,@RequestParam("bol")Integer bol,@RequestParam("picid") String picid) {
//        System.out.println("bol=="+bol);
//        System.out.println(sid);
//        System.out.println(bol);
//        System.out.println(price);
//        System.out.println(count);
        Gstock goods = gstockService.getOne(new QueryWrapper<Gstock>().eq(
                "ssid", sid
        ));
        Goods goods2 = goodsService.getOne(new QueryWrapper<Goods>().eq(
                "picid", picid
        ));
        System.out.println("goods2"+goods);
        Goods goods1 = new Goods();
        goods1.setPrice(BigDecimal.valueOf(0));
        goods1.setType(goods.getType());
        goods1.setBrand(goods.getBrand());
        goods1.setPicid(goods.getPicid());
        goods1.setGoodsname(goods.getGoodsname());
        goods1.setProducingArea(goods.getProducingArea());
        goods1.setQuantity(0);
        goods1.setQuality(goods.getQuality());
        if (bol == 0){
            goods.setIsup(0);
            int bl = goodsMapper.delete(new QueryWrapper<Goods>().eq(
                    "picid", picid
            ));
        }else{
            goods.setIsup(1);
            goodsService.save(goods1);
        }
        gstockService.updateById(goods);
        return Result.success(200,"删除",null);

    }
    @RequestMapping("/upgoo")
    public Result upgoo(@RequestBody goodsDto goodsDto) {
        System.out.println(goodsDto);
        Gstock goods = new Gstock();
        goods.setGoodsname(goodsDto.getGoodsname());
        goods.setType(goodsDto.getType());
        goods.setQuality(goodsDto.getQuality());
        goods.setBrand(goodsDto.getBrand());
        goods.setPrice(goodsDto.getPrice());
        goods.setPrice(goods.getPrice());
        goods.setProducingArea(goodsDto.getProducingArea());
        goods.setPicid(goodsDto.getPicid());

        boolean flag = gstockService.save(goods);
        if (flag){
            boolean flag2;
            return Result.success(200,"提交成功",null);
        }
        else {
            return Result.fail(400,"失败",null);
        }
    }
    @RequestMapping("/upkc")
    public Result upkc(@RequestParam("ssid")int ssid,@RequestParam("count") Integer count) {
        System.out.println(ssid);
        Gstock gstock = gstockService.getOne(new QueryWrapper<Gstock>().eq(
                "ssid",ssid
        ));
        int i = gstock.getStock();
        gstock.setStock(i+count);
      boolean flag = gstockService.updateById(gstock);
      if (flag){
          return Result.success(200,"更新数量成功",null);
      }else {
          return Result.fail(400,"更新失败",null);
      }
    }
    @RequestMapping("/editgoods")
    public Result editgoods(@RequestBody goodsDto goodsDto) {
        System.out.println(goodsDto);
        Goods goods = new Goods();
        Gstock gstock = new Gstock();
        gstock.setSsid(goodsDto.getSsid());
        gstock.setType(goodsDto.getType());
        gstock.setQuality(goodsDto.getQuality());
        gstock.setProducingArea(goodsDto.getProducingArea());
        gstock.setPrice(goodsDto.getPrice());
        gstock.setBrand(goodsDto.getBrand());
        gstock.setGoodsname(goodsDto.getGoodsname());
        System.out.println(goodsDto.getPicid());
        Goods goods1 = goodsService.getOne(new QueryWrapper<Goods>().eq(
                "picid",goodsDto.getPicid()
        ));
        System.out.println(goods1);
        goods1.setType(goodsDto.getType());
        goods1.setQuantity(goodsDto.getQuantity());
        goods1.setBrand(goodsDto.getBrand());
        goods1.setPrice(goodsDto.getPrice());
        goods1.setProducingArea(goodsDto.getProducingArea());
        goods1.setQuality(goodsDto.getQuality());
        goods1.setGoodsname(goodsDto.getGoodsname());


        boolean flag2 = gstockService.updateById(gstock)&&goodsService.updateById(goods1);
        if (flag2){
            return Result.success(200,"成功",null);
        }else {
            return Result.fail(400,"失败",null);
        }


    }
}
